﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using UnityEngine;

namespace Hydrogen.MarkovChain
{
    public static class Utils
    {
        #region Random
        /// <summary>
        /// 打乱一个列表的顺序
        /// </summary>
        public static void RandomArray<T>(List<T> list)
        {
            T tempVal;
            int randomIndex;
            for (int i = 0; i < list.Count; i++)
            {
                randomIndex = UnityEngine.Random.Range(0, list.Count);

                tempVal = list[i];
                list[i] = list[randomIndex];
                list[randomIndex] = tempVal;
            }
        }
        #endregion

        #region SolidType

        public static char Solid2Char(SolidType type)
        {
            switch (type)
            {
                case SolidType.Any:
                    return '*';
                case SolidType.None:
                    return '0';
                default:
                    return type.ToString()[0];
            }
        }

        public static List<SolidType> Str2SolidType(string str)
        {
            List<SolidType> solidTypes = new List<SolidType>();
            if (string.IsNullOrEmpty(str)) return solidTypes;
            for (int i = 0; i < str.Length; i++)
            {
                solidTypes.Add(Utils.Char2SolidType(str[i]));
            }
            return solidTypes;
        }

        public static SolidType Char2SolidType(char c)
        {
            switch (c)
            {
                case '*': return SolidType.Any;
                case '0': return SolidType.None;
                default:
                    if (Enum.TryParse(c.ToString(), out SolidType result)) return result;
                    return SolidType.None;
            }
        }
        #endregion

        #region Direction
        public static Vector2Int GetPosByDir(Vector2Int curr, Direction dir)
        {
            switch (dir)
            {
                case Direction.U:
                    curr.y++;
                    break;
                case Direction.D:
                    curr.y--;
                    break;
                case Direction.L:
                    curr.x--;
                    break;
                case Direction.R:
                    curr.x++;
                    break;
            }
            return curr;
        }
        #endregion

        #region Xml
        public static string GetAttributeValue(this XmlNode node, string attributeName)
        {
            if (node == null) return null;
            return node.Attributes?.GetNamedItem(attributeName)?.Value;
        }
        public static int GetAttributeValueInt(this XmlNode node, string attributeName, int defaultValue)
        {
            if (node == null) return defaultValue;
            var str = node.Attributes?.GetNamedItem(attributeName)?.Value;
            if (string.IsNullOrEmpty(str)) return defaultValue;
            if(!int.TryParse(str, out int result)) result = defaultValue;
            return result;
        }
        #endregion

        #region Vector
        public static Vector3Int Str2V3(string str)
        {
            Vector3Int result = Vector3Int.zero;
            if (string.IsNullOrEmpty(str)) return result;

            var split = str.Split(',');
            if (split.Length < 2) return result;// z可以不写，不写就是0

            if (!int.TryParse(split[0], out int x)) return result;
            if (!int.TryParse(split[1], out int y)) return result;
            if (split.Length <= 2 || !int.TryParse(split[2], out int z)) z = 0;// z可以不写，不写就是0

            result.x = x;
            result.y = y;
            result.z = z;

            return result;
        }

        public static Vector2Int ConvertV2(this Vector3Int v3) => new Vector2Int(v3.x, v3.y);
        #endregion
    }
}
